<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *会员列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
		array('op'=>'list','title'=>'导入操作日志'),
		array('op'=>'edit','title'=>'添加导入'),
);
//会员列表
if($_RQ['op']=='list'){
	$params=array('merchid'=>$_SESSION['onemerchid']);
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_scrm_member_daoru',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=array('username'=>$item['username'],'avatar'=>$item['avatar'],'nickname'=>$item['nickname']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：在合规的情况下，以手机号作为客户的线索，下载csv模板表格后输入手机号上传。上传后，表格内手机号将平均分配给选择的员工，分配完成后员工会在企业微信收到添加好友的任务，需要员工手动添加客户。
		','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'title','title'=>'csv名称','type'=>'string'),
			array('name'=>'createtime','title'=>'上传时间','type'=>'time'),
			array('name'=>'workerid','title'=>'分配员工','type'=>'select','options'=>'workers','option_title'=>'name'),
			array('name'=>'membernum','title'=>'总人数','type'=>'string'),
			array('name'=>'subnum','title'=>'导入成功的人数','type'=>'string'),
			array('name'=>'failnum','title'=>'导入失败的人数','type'=>'string'),
		),'action'=>array(
			//array('type'=>'post','action'=>'updata','title'=>'导入数据'),
		)),
		'search_one'=>array('name'=>'title','add_op'=>'edit'),
	);
	$return['workers']=$this->getall('worker',array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']),array('id','name'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_scrm_member_daoru',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回列表'),
		),
		'edit'=>array(
					array('type'=>'tip','title'=>'<a href="../addons/jy_scrm/resource/import_customer.csv" class="green">下载csv模板</a>。请按照模板的要求上传客户资料，若有重复手机号或空行将会自动过滤，以及不符合规范的填写格式，上传后直接显示”--“'),
					array('type'=>'upfile','title'=>'选择表格','name'=>'excel','help'=>''),
					array('name'=>'app_id','title'=>'选择应用','type'=>'choose','action'=>'core/wxwork.app/get_app/','help'=>'关联应用后，员工在企业微信会收到添加好友（分配的客户）的任务通知。'),
					array('name'=>'workerid','title'=>'分配员工','type'=>'choose','action'=>'jy_scrm/worker.get_worker/','help'=>'表格内手机号将平均分配给选择的员工'),
		)
	);	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['excel']){
		exi('请上传表格','error');
	}
	if(!$postdata['app_id']){
		exi('请选择应用','error');
	}
	if(!$postdata['workerid']){
		exi('请分配员工','error');
	}
	$data=array(
		'workerid'=>intval($postdata['workerid']),
		'app_id'=>intval($postdata['app_id']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_scrm_member_daoru',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$excel=cfc('files')->local($postdata['excel']);
		$excel=cfc('tools')->importExcel($excel,true);		
		cfc('files')->source_del($postdata['excel']);
		//$res=$this->model('model')->get_csvitems($excel);		
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$_SESSION['onemerchid'];
		$data['createtime']=time();
		$data['subnum']=count($excel)-1;
		$data['membernum']=$data['subnum'];
		pdo_insert('jy_scrm_member_daoru',$data);
		unset($excel[0]);
		foreach($excel as $v){
			$member=array(
				'mobile'=>$v[0],
				'name'=>$v[1],
				'userid'=>$v[2],
				'gender'=>$v[3],
				'pcd'=>$v[4],
				'remark'=>$v[5],
				'follow_user_userid'=>$postdata['workerid'],
			);
			$this->model('member')->create($member);
		}
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_scrm_member_daoru',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择会员！','error');
	}
	$post=get_postdata();
	if(isset($post['isblack'])){
		pdo_update('jy_scrm_member',array('isblack'=>intval($post['isblack'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='updata'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择任务！','error');
	}
	$item=pdo_get('jy_scrm_member_daoru',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$data=cfc('tools')->importExcel($item['excel']);
	if(!$data){
		exi('数据读取失败~','error');
	}
	$first=false;
	foreach($data as $row){
			if(!$first){
				$first=true;
				continue;
			}
			$pcd=str_array($row['4'],'-');
			$pcd=array('province'=>$pcd[0],'city'=>$pcd[2]);
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'merchid'=>$_SESSION['onemerchid'],
				'workerid'=>$item['workerid'],
				'external_userid'=>'',
				'mobile'=>$row['0'],
				'name'=>$row['2'],
				'nickname'=>$row['1'],
				'gender'=>$row['3']=='男'?1:2,
				'pcd'=>ijson_encode($pcd),
				'remark'=>$row['6'],
				'createtime'=>TIMESTAMP,
			);
			$check=pdo_getcolumn('jy_scrm_member',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$insert['mobile']),'id');
			if(!$check){
				pdo_insert('jy_scrm_member',$insert);
			}
	}
	exi('执行成功！');
}